<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 *
 *
 *
 * @package     h
 * @subpackage  models
 * @category    models
 * @author      hyw
 * @copyright   copyright (c) 2012
 * @filesource
 */
class Mrt_model extends Base_model {
    protected $_table_name = 'mrt';
    protected $_key_name = 'mrt_id';
	
	function get_info($id) {
		if(!empty($id))
			$where[$this->_key_name]=$id;
		$where['dtime']=0;
		return $this->fetch(NULL, $where,NULL,'row');
	}

    function get_all() {
		$this->set_order_by('sequence,ASC');
		$where=array('dtime'=>0);
		return $this->fetch(NULL, $where);
    }

	function get_all_count() {
		$where=array('dtime'=>0);
		return $this-> fetch(NULL, $where, NULL,"count");
    }

	function get_not_in($i){
		$v=explode(",",$i);
		$this->db->where_not_in('value',  array_map('trim',$v));
		$this->set_order_by('sequence,ASC');
		$where=array('dtime'=>0);
		return $this->fetch(NULL, $where);
	}
	
	function get_mrt() {
		$this->set_order_by('sequence,ASC');
		$where['dtime']=0;
		$result=$this->fetch(NULL, $where);
		$p=array();
		foreach($result as $key=>$val){
			$p[$val->mrt_id]=$val->value;
		}
		return $p;
    }

	function get_mrt_by_mrt_line() {
		$this->set_order_by('sequence,ASC');
		$where['dtime']=0;
		$result=$this->fetch(NULL, $where);
		$p=array();
		foreach($result as $key=>$val){
			$p[$val->mrt_line_id][$val->mrt_id]=$val->value;
		}
		return $p;
    }
	
	function get_mrt_by_id($id) {
		$this->set_order_by('sequence,ASC');
		$where['mrt_line_id']=$id;
		$where['dtime']=0;
		return $this->fetch(NULL, $where);
    }

	function query_keyword($keyword="",$limit) 
	{
		if($keyword!="")
			$this->set_or_like('value,explanation',$keyword);
		$this->set_limit($limit);
		$where=array('dtime'=>0);
		return $this->fetch(NULL, $where);
    }
	
	function query_keyword_count($keyword=null)
	{
		if($keyword!="")
			$this->set_or_like('value,explanation',$keyword);
		$con["where"][] = array('dtime'=>0);
        $this->set_args_array($con);
        return $this->fetch_count();
	}
	
	function update_data($data) {
		$value=array('value'=>$data['value'],
					'mrt_line_id'=>$data['mrt_line_id'],
					'explanation'=>$data['explanation'],
					'sequence'=>$data['sequence'],
					'code'=>$data['code'],
					'mtime'=>time());
		$where[]=array($this->_key_name=>$data['id']);
		return $this->update($value, $where);
	}

	function add($data) {		
		
		$this->db->insert ( 'h_mrt',$data );
		//return 1;
	}

	function del($data) {
		$where[]=array($this->_key_name=>$data['id']);
		$value=array('dtime'=>time());
		return $this->update($value, $where);
	}
	
}